package icu.zlz.message.config;

import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.JavaMailSenderImpl;

/**
 * 邮件配置
 */
@Configuration
@EnableConfigurationProperties({MailProperties.class, SecondaryMailProperties.class})
public class MailConfig {
    private final MailProperties mailProperties;
    private final SecondaryMailProperties secondaryMailProperties;

    public MailConfig(MailProperties mailProperties, SecondaryMailProperties secondaryMailProperties) {
        this.mailProperties = mailProperties;
        this.secondaryMailProperties = secondaryMailProperties;
    }

    @Bean
    @Qualifier("primaryJavaMailSender")
    public JavaMailSender primaryJavaMailSender() {
        JavaMailSenderImpl mailSender = new JavaMailSenderImpl();
        mailSender.setHost(mailProperties.getHost());
        mailSender.setPort(mailProperties.getPort());
        mailSender.setUsername(mailProperties.getUsername());
        mailSender.setPassword(mailProperties.getPassword());
        mailSender.setJavaMailProperties(mailProperties.getProperties());
        return mailSender;
    }

    @Bean
    @Qualifier("secondaryJavaMailSender")
    public JavaMailSender secondaryJavaMailSender() {
        JavaMailSenderImpl mailSender = new JavaMailSenderImpl();
        mailSender.setHost(secondaryMailProperties.getHost());
        mailSender.setPort(secondaryMailProperties.getPort());
        mailSender.setUsername(secondaryMailProperties.getUsername());
        mailSender.setPassword(secondaryMailProperties.getPassword());
        mailSender.setJavaMailProperties(secondaryMailProperties.getProperties());
        return mailSender;
    }
}
